package factorytest.iflytek.com.einkiflytekfactorytest.agingtest;

import android.content.Context;
import android.os.Message;
import android.os.PowerManager;
import android.view.View;

import androidx.annotation.NonNull;
import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.DebugLog;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.TimeStringUtils;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.UsefulUtils;

public class TestReboot extends AgingTestItemBase {

    private static final int MSG_COUNT_DOWN = 100;
    private static final int PERIOD_COUNT_DOWN = 5000;
    private static final int MSG_REBOOT = 200;

    private int countDown = 20;

    public TestReboot(@NonNull Context context, @NonNull View view) {
        super(context, view);

        if (getAgingTestItemAttr() != null
                && getAgingTestItemAttr().isTestOver(context)
                && AgingTestItemAttr.isRebootTestStarted(getContext())) {

            super.setTestStarted(true);
        }
    }

    @Override
    protected void handleMessage(Message msg) {
        final int what = msg.what;
        switch (what) {
            case MSG_COUNT_DOWN:
                updateUI();
                break;
            case MSG_REBOOT:
                reboot();
                break;
        }
    }

    @Override
    protected void doStartTest() {
        DebugLog.e("TestReboot doStartTest");
        updateUI();
    }

    @Override
    protected void doStopTest() {
        DebugLog.e("TestReboot doStopTest");
        getHandler().removeMessages(MSG_COUNT_DOWN);
    }

    @Override
    public AgingTestItemAttr getAgingTestItemAttr() {
        return AgingTestItemAttr.reboot;
    }

    @Override
    protected void setTestStarted(boolean started) {
        super.setTestStarted(started);
        AgingTestItemAttr.setRebootTestStarted(getContext(), started);
    }

    private void updateUI() {

        countDown -= PERIOD_COUNT_DOWN / 1000;

        long usedTime = AgingTestItemAttr.getAgingTimeTick_ms(getContext()) - getAgingTestItemAttr().getStartTime_ms(getContext());

        String timeStr = TimeStringUtils.makeTimeString(usedTime);

        String info = getContext().getString(R.string.aging_reboot_info, countDown,
                getAgingTestItemAttr().getTestCount(getContext()),
                timeStr);

        long startTimeOfReboot = getAgingTestItemAttr().getStartTimeAbsolute_ms(getContext());
        long now = System.currentTimeMillis();

        String timeInfo = getContext().getString(R.string.aging_reboot_time_info,
                UsefulUtils.millis2String(startTimeOfReboot),
                UsefulUtils.millis2String(now));

        updateTxtItemInfo(info + "\n" + timeInfo);

        if (countDown <= 0) {
            getHandler().sendEmptyMessage(MSG_REBOOT);
        } else {
            getHandler().sendEmptyMessageDelayed(MSG_COUNT_DOWN, PERIOD_COUNT_DOWN);
        }
    }

    private void reboot() {

        int count = getAgingTestItemAttr().getTestCount(getContext());
        count++;
        getAgingTestItemAttr().setTestCount(getContext(), count);

        PowerManager pManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
        if (pManager != null) {
            pManager.reboot(null);
            DebugLog.e("reboot now");
        }
    }
}
